<?php
namespace app\admin\controller;
use think\Db;
//use Qiniu\Config as Config1;
//use Qiniu\Storage\BucketManager;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;
//use think\Image;
class Banner extends Common
{
	//等级列表
    public function banner_list()
    {
		$where="a.b_id > 0";
		$name=Request()->param('name');
		$status=Request()->param('status');
		$start_time=Request()->param('start_time');
		$end_time=Request()->param('end_time');
		$page=Request()->param('page');
		$size=Request()->param('size');
		if($page<1){
			$page=1;
		}
		if($size<15){
			$size=15;
		}
		$now=($page-1)*$size;
		if($name!=''){
			$where.=" and a.b_title like '%".$name."%'";
		}
		if($status>0){
			if($status==1){
				$where.=" and a.b_status = 1";
			}else{
				$where.=" and a.b_status != 1";
			}
			
		}
		if(strtotime($start_time)>946656000){
			$start_time=strtotime($start_time);
			$where.=" and a.b_create_time >= $start_time";
		}else{
			$start_time=0;
		}
		if(strtotime($end_time)>946656000){
			$end_time=strtotime($end_time);
			$where.=" and a.b_create_time <= $end_time";
		}else{
			$end_time=0;
		}
		$data=Db::name('banner a')
			->where($where)
			->limit("$now,$size")
			->order("a.b_sort desc")
			->select();
		$count=Db::name('banner a')
			->where($where)
			->count();
		//等级信息
		$page_str=page($page,$size,$count,count($data));//分页
		$this->assign('data',$data);
		$this->assign('count',$count);
		$this->assign('name',$name);
		$this->assign('status',$status);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('page',$page);
		$this->assign('size',$size);
		$this->assign('page_str',$page_str);
		return $this->fetch();
    }
	//添加banner
	function add_banner(){
		if(Request()->isAjax()){
			$permissions=$this->check_permissions();
			if($permissions['code']!=200){
				return $permissions;die;
			}
			$data=Request()->post();
			/*$file =Request()->file("b_img");
			if(!$file){
				return return_json(array('code'=>0,'msg'=>'请选择图片！'),'添加banner');
				// form_json(array('code'=>0,'msg'=>'请选择图片！'),'添加banner');
				// echo '<script type="text/javascript">alert("请选择图片");history.go(-1);</script>';die;
			}*/
            $file = request()->file('b_img');

            if(!$file){
                return return_json(array('code'=>0,'msg'=>'请选择图片！'),'添加banner');
            }
			$title=$data['b_title'];
			$url=$data['b_url'];
			$type=$data['b_type'];
			$order=$data['b_sort'];
			if(isset($data['b_status'])){
				$is_enable=1;
			}else{
				$is_enable=0;
			}
			if($title==''){
				return return_json(array('code'=>0,'msg'=>'请输入标题！'),'添加banner');
				// form_json(array('code'=>0,'msg'=>'请输入标题！'),'添加banner');
				// echo '<script type="text/javascript">alert("请输入标题");history.go(-1);</script>';die;
			}
            //以下为图片上传到其他地址(阿里云)
            $upload = new Upload();
            $return=$upload->upload($file);
            if($return['code']!=1){
                return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
            }else{
                $img=$return['url'];
            }
            //以上为图片上传到其他地址(阿里云)

            //以下为图片上传到其他地址(七牛云)
//            $qiniu_config = config('qiniu');
//            $accessKey    = $qiniu_config['ACCESSKEY'];
//            $secretKey    = $qiniu_config['SECRETKEY'];
//            $info = $file->validate(['size'=>1024*1024*3,'ext'=>'jpeg,jpg,png,gif']);
//            if (!$info){
//                return return_json(array('code'=>0,'msg'=>$file->getError()),'添加banner');
//            }
//            $filePath = $file->getRealPath();
//            $fileName = input('fileName');
//            if (!$fileName){
//                $ext   = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
//                $fileName   = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
//            }
//            $auth  = new Auth($accessKey, $secretKey);
//            $token = $auth->uploadToken($qiniu_config['bucket']);
//
//            $uploadMgr = new UploadManager();
//            list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath);
//            if ($err !== null) {
//                return return_json(array('code'=>0,'msg'=>'图片上传失败'),'添加banner');
//            }
//            $img=config('qiniu.domain').$ret['key'];
            //以上为图片上传到其他地址
            //以下为图片上传到本地
//            $info = $file->move(ROOT_PATH.'public/upload/banner'.DS);
//            $img="/upload/banner".DS.$info->getSaveName();
            //以上为图片上传到本地(七牛云)
			$res=Db::name('banner')
				->insert(array('b_title'=>$title,'b_mold'=>$data['b_mold'],'b_url'=>$url,'b_type'=>$type,'b_sort'=>$order,'b_status'=>$is_enable,'b_img'=>$img,'b_create_time'=>time(),'b_add_user_id'=>session('uid'),'b_add_user_name'=>session('user.real_name')));
			if($res){
				return return_json(array('code'=>1,'msg'=>'添加成功！'),'添加banner');
				// form_json(array('code'=>200,'msg'=>'添加成功！'),'添加banner');
				// echo '<script type="text/javascript">alert("添加成功");parent.location.reload();</script>';die;
			}else{
				return return_json(array('code'=>0,'msg'=>'添加失败！'),'添加banner');
				// form_json(array('code'=>0,'msg'=>'添加失败！'),'添加banner');
				// echo '<script type="text/javascript">alert("添加失败请重试");history.go(-1);</script>';die;
			}
			
		}else{
			return $this->fetch();
		}
	}
	//banner冻结
	function banner_del(){
		$permissions=$this->check_permissions();
		if($permissions['code']!=200){
			return $permissions;die;
		}
		//ajax必须写上这些（ajax判断权限）
		if(Request()->isAjax()){
			$id=Request()->post('id');
			$res=Db::name('banner')
				->where(array('b_id'=>$id))
				->update(array('b_status'=>0,'b_update_time'=>time()));
			if($res){
				return return_json(array('code'=>1,'msg'=>'冻结成功！'),'冻结banner');die;
			}else{
				return return_json(array('code'=>0,'msg'=>'冻结失败！'),'冻结banner');die;
			}
		}
	}
	//banner解冻
	function banner_use(){
		//ajax必须写上这些（ajax判断权限）
		if(Request()->isAjax()){
			$permissions=$this->check_permissions();
			if($permissions['code']!=200){
				return $permissions;die;
			}
			$id=Request()->post('id');
			$res=Db::name('banner')
				->where(array('b_id'=>$id))
				->update(array('b_status'=>1,'b_update_time'=>time()));
			if($res){
				return return_json(array('code'=>1,'msg'=>'解冻成功！'),'解冻banner');die;
			}else{
				return return_json(array('code'=>0,'msg'=>'解冻失败！'),'解冻banner');die;
			}
		}
	}
	//添加banner
	function rebanner(){
		if(Request()->isAjax()){
			$permissions=$this->check_permissions();
			if($permissions['code']!=200){
				return $permissions;die;
			}
			$data=Request()->post();
			$id=$data['id'];
			$file =Request()->file("b_img");
			if($file){
//				$info = $file->move(ROOT_PATH.'public/upload/banner'.DS);
//				$img="/upload/banner".DS.$info->getSaveName();
                //以下为图片上传到其他地址(阿里云)
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
                }else{
                    $img=$return['url'];
                }
                //以上为图片上传到其他地址(阿里云)
			}else{
				$img='';
			}
			$title=$data['b_title'];
			$url=$data['b_url'];
			$type=$data['b_type'];
			$order=$data['b_sort'];
			if(isset($data['b_status'])){
				$is_enable=1;
			}else{
				$is_enable=0;
			}
			if($title==''){
				return return_json(array('code'=>0,'msg'=>'请输入标题！'),'修改banner');
				// form_json(array('code'=>0,'msg'=>'请输入标题！'));
				// echo '<script type="text/javascript">alert("请输入标题");history.go(-1);</script>';die;
			}
			$data1=array('b_title'=>$title,'b_mold'=>$data['b_mold'],'b_url'=>$url,'b_type'=>$type,'b_sort'=>$order,'b_status'=>$is_enable,'b_update_time'=>time());
			if($img!=''){
				$data1['b_img']=$img;
			}
			$res=Db::name('banner')
				->where(array('b_id'=>$id))
				->update($data1);
			if($res!=false){
				return return_json(array('code'=>1,'msg'=>'修改成功！'),'修改banner');
				// form_json(array('code'=>1,'msg'=>'修改成功！'));
				// echo '<script type="text/javascript">alert("修改成功");parent.location.reload();</script>';die;
			}else{
				return return_json(array('code'=>0,'msg'=>'修改失败！'),'修改banner');
				// form_json(array('code'=>0,'msg'=>'修改失败！'));
				// echo '<script type="text/javascript">alert("修改失败请重试");history.go(-1);</script>';die;
			}
			
		}else{
			$id=Request()->get('id');
			$banner=Db::name('banner')
				->where(array('b_id'=>$id))
				->find();
			$this->assign('id',$id);
			$this->assign('banner',$banner);
			return $this->fetch();
		}
	}
}